from app.utils.oss import OSSClient


class OSSClientFlask(object):
    def __init__(self, app=None):
        self._oss_client = None
        self.app = app
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        oss_app_domain = app.config.get("OSS_APP_DOMAIN")
        oss_auth_domain = app.config.get("OSS_AUTH_DOMAIN")
        oss_app_id = app.config.get("OSS_APP_ID")
        oss_app_key = app.config.get("OSS_APP_KEY")
        oss_bucket = app.config.get("OSS_BUCKET")
        self._oss_client = OSSClient(app_domain=oss_app_domain, auth_domain=oss_auth_domain, app_id=oss_app_id, \
                                     app_key=oss_app_key, bucket=oss_bucket);
        if not hasattr(app, "extensions"):
            app.extensions = {}

        app.extensions["oss_client"] = self

    def createCert(self):
        if self._oss_client is not None:
            return self._oss_client().createCert()
        else:
            raise AssertionError("OSSClient 未出初始化")

    def upload(self, object_name, object_path):
        if self._oss_client is not None:
            return self._oss_client.upload(object_name, object_path)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def download(self, object_name, download_path):
        if self._oss_client is not None:
            return self._oss_client.download(object_name, download_path)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def getPresignedUrl(self, object_name):
        if self._oss_client is not None:
            return self._oss_client.getPresignedUrl(object_name)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload_init(self, object_name, cert=None):
        if self._oss_client is not None:
            return self._oss_client.multiupload_init(object_name, cert)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload_upload(self, object_name, object_part_path, files_num, upload_id,cert=None):
        if self._oss_client is not None:
            return self._oss_client.multiupload_upload(object_name, object_part_path, files_num, upload_id,cert)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload_complete(self, object_name, upload_id, parts, cert=None):
        if self._oss_client is not None:
            return self._oss_client.multiupload_complete(object_name, upload_id, parts, cert)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload_abort(self, object_name, upload_id, cert=None):
        if self._oss_client is not None:
            return self._oss_client.multiupload_abort(object_name, upload_id, cert)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload_list(self, object_name, uploadid, cert=None):
        if self._oss_client is not None:
            return self._oss_client.multiupload_list(object_name, uploadid, cert)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload_validate(self, upload_results, upload_list):
        if self._oss_client is not None:
            return self._oss_client.multiupload_validate(upload_results, upload_list)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def multiupload(self, object_name, object_path, threads_num=8):
        if self._oss_client is not None:
            return self._oss_client.multiupload(object_name, object_path, threads_num)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def delete(self, object_name, cert=None):
        if self._oss_client is not None:
            return self._oss_client.delete(object_name, cert)
        else:
            raise AssertionError("OSSClient 未出初始化")

    def delete_list(self, object_name_list, cert=None):
        if self._oss_client is not None:
            return self._oss_client.delete_list(object_name_list, cert)
        else:
            raise AssertionError("OSSClient 未出初始化")
